import { createRouter,createWebHistory } from "vue-router"
import { ElMessage } from "element-plus";
import axios from "axios";

const routers = [
    {
        path:"/",
        meta:{
            //自定义路由属性
            outPro:true
        },
        component:()=>import("@/view/login/loginAndRegister.vue")
    },{
        path:"/login",
        meta:{
            //自定义路由属性
            outPro:true
        },
        component:()=>import("@/view/login/loginAndRegister.vue")
    },{
        path:"/layout",
        component: ()=>import("@/view/layoutView.vue"),
        children:[
            {
                path:"userIndex",
                component:()=>import("@/view/user/userIndexView.vue")
            },{
                path:"client",
                component:()=>import("@/view/client/clientView.vue"),
                children:[
                    {
                        path: "recycleBin",
                        component: ()=>import("@/view/client/recycleBin")
                    },{
                        path: "clientDefault",
                        component: ()=>import("@/view/client/clientDefault")
                    }
                ]
            },{
                path:"insuranceProduct",
                component:()=>import("@/view/insurance/insuranceProduct.vue")
            },{
                path:"insurancePolicy",
                component:()=>import("@/view/insurance/insurancePolicy.vue")
            },{
                path:"insuranceClaim",
                component:()=>import("@/view/insurance/insuranceClaimView.vue")
            },{
                path: "userInformation",
                component:()=>import("@/view/user/userInformationView.vue")
            }
        ]
    },{
        path: "/test",
        component:()=>import("@/view/test/testView.vue")
    }

]

const MyRouter = createRouter({
    history:createWebHistory(process.env.BASE_URL),
    routes:routers
})

// 路由守卫
MyRouter.beforeEach(function(to,from,next){
    if(to.meta.outPro === true){
        next();
    }else{
        axios({
            method: "GET",
            url: "http://localhost:8080/boot/user/getSession"
        }).then((result)=>{
            if(result.data !== '' && result.data != null){
                next();
            }else{
                ElMessage.error('用户未登录，请先登录')
                MyRouter.replace("/login")
            }
        })
    }
})

export default MyRouter